package mt521.cam.ac.uk;

import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;

public class TESTSmisc {
    
    private static double backgroundLevel = 0.0;
    private static int chunkSize = Encode.getChunkSize();

    public static void main(String[] args){
        byte[] array1 = Encode.buffer(3000);
        byte[] array2 = Encode.buffer(4000);
        byte[] array3 = Encode.buffer(5000);
        System.out.println("Lengths are "+array1.length+", "+array2.length+" and "+array3.length);
        byte[] array = new byte[array1.length*4];
        for (int i=0; i<array1.length; i++) {
            array[i]=array1[i];
            array[i+array1.length]=array2[i];
            array[i+2*array1.length]=array3[i];
        }
        double returned = Decode.decode(array);
        System.out.println("Frequency heard: "+returned);
    }
    
    /*
    public static void main(String[] args) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        double[] dTone = new double[chunkSize];
        double[] transformed = new double[chunkSize];
        long lFreq;
        byte bFreq;
        
        File file = new File("micData.txt");
        byte[] bTone = FileOperators.convertToBytes(file);
        
        for (int i=0; i<512; i++) {
            System.out.print(bTone[i]+" ");
        }
        
        //System.out.println("Sound Captured.");
        backgroundLevel = getBackground(bTone);
        
        for(int i=chunkSize; i<bTone.length; i+=chunkSize) {    //first 512 used in comparison, required to never be data
            for(int j = 0; j<chunkSize; j++) {
                dTone[j] = (double) bTone[i+j];
            }
            transformed = Decode.fft(dTone);
            if (isData(transformed)){
                lFreq = (Decode.getFrequency(transformed)-300)/30;
                bFreq = lFreq>127 ? (byte) (lFreq-256) : (byte) lFreq;
                stream.write(bFreq);
            }
        }
        //System.out.println("Sound Decoded.");
        byte[] result = stream.toByteArray();
        FileOperators.convertToFile(result,"dolphin");
        //System.out.println("New file saved to dolphin.txt");
    }*/
    
    public static double getBackground(byte[] bTone) {
        double max = 0.0;
        double[] dTone = new double[chunkSize];
        for(int j = 0; j<chunkSize; j++) {
            dTone[j] = (double) bTone[j];
        }
        double[] transformed = Decode.fft(dTone);       //is half the size as second half ignored
        for (int i=0; i<transformed.length; i++) {
            max = transformed[i]>max ? transformed[i] : max;
        }
        return max*1.2;      //allow a small tolerance, up to one fifth higher than the sample.
    }
    
    public static boolean isData(double[] amp) {
        double max = 0.0;
        for (int i=0; i<amp.length; i++) {
            max = amp[i]>max ? amp[i] : max;
        }
        if (max>backgroundLevel) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void findMicrophone() {
        Mixer.Info[] mi = AudioSystem.getMixerInfo();
        for(Mixer.Info i : mi) {
            System.out.println("Name: "+i.getName());
            System.out.println("Descr: "+i.getDescription());
            System.out.println();
        }
        byte[] microphone = FileOperators.mic(1000);
        File save = new File("mictest.wav");
        Output.save(microphone,save,2000);
    }

}
